using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.ComponentModel;
using Axe.Extensions;

namespace Axe.Web.Controls
{
    public delegate void OnRequestDataItems(HardcodedDropDownList list);

    public class HardcodedDropDownListEventArgs : EventArgs
    {
        Collection<ListItem> m_BoundItems = new Collection<ListItem>();

        public Collection<ListItem> BoundItems
        {
            get { return m_BoundItems; }
        }
    }

    public class HardcodedDropDownList : DropDownList
    {
        public HardcodedDropDownList()
        {
            AppendDataBoundItems = true;
        }

        [DefaultValue(false)]
        public bool AppendEmptyItem
        {
            get { return ViewState.Get("AppendEmptyItem", false); }
            set { ViewState["AppendEmptyItem"] = value; }
        }

        [DefaultValue(false)]
        public bool SelectEmptyText
        {
            get { return ViewState.Get("SelectEmptyText", false); }
            set { ViewState["SelectEmptyText"] = value; }
        }

        #region public override string DataTextField
        public override string DataTextField
        {
            get
            {
                return "Text";
            }
        } 
        #endregion

        #region public override string DataValueField
        public override string DataValueField
        {
            get
            {
                return "Value";
            }
        } 
        #endregion

        protected override void OnPreRender(EventArgs e)
        {
            try
            {
                if (tmpSelectedValue != null)
                    base.SelectedValue = tmpSelectedValue;
            }
            catch (Exception exc)
            {
                Debug.WriteLine(exc.Message);
            }

            base.OnPreRender(e);
        }
       
        protected override void EnsureDataBound()
        {
            if (RequiresDataBinding)
            {
                HardcodedDropDownListEventArgs args = OnRequestDataItems();

                if (AppendEmptyItem)
                {
                    args.BoundItems.Insert(0, new ListItem("","",SelectEmptyText));
                }

                DataSource = args.BoundItems;
                DataBind();
            }
        }
        

        bool m_IsDataBoundCalled = false;

        public override void DataBind()
        {
            if (!m_IsDataBoundCalled)
            {
                m_IsDataBoundCalled = true;
                base.DataBind();
            }
        }

        public void ForceDataBind()
        {
            ClearChildViewState();
            ClearChildState();
            RequiresDataBinding = true;
            EnsureDataBound();
        }

        string tmpSelectedValue = null;

        [Bindable(true, BindingDirection.TwoWay)]
        public override string SelectedValue
        {
            get
            {
                return base.SelectedValue;
            }
            set
            {
                if (Items.Count == 0)
                    EnsureDataBound();

                if (Items.Count > 0)
                    base.SelectedValue = value;
                else
                    tmpSelectedValue = value;

            }
        }

        protected virtual HardcodedDropDownListEventArgs OnRequestDataItems()
        {
            HardcodedDropDownListEventArgs args = new HardcodedDropDownListEventArgs();

            if (RequestDataItems != null)
            {
                RequestDataItems(this, args);
            }

            return args;
        }

        public event EventHandler<HardcodedDropDownListEventArgs> RequestDataItems;
    }
}
